package com.game.chinesechess;

import com.game.chinesechess.check.rangeSense;
import  com.game.chinesechess.piece.chess;
import  com.game.chinesechess.piece.specific.*;
import com.game.chinesechess.util.Point;
import com.game.chinesechess.util.information;
import ohos.agp.window.dialog.CommonDialog;
import ohos.global.configuration.DeviceCapability;

public class GameInfo {
    public chess[][] board = new chess[10][9];
    public chess.Color turn;
    public chess.Color Winner = null;
    public state next_step=state.SELECT;
    public chess.Color color;
    public I i;

    public GameInfo() {
        turn = chess.Color.RED;
        for (int i = 0; i < 5; i++) {
            chess.Color color = chess.Color.BlACK;
            if (i == 0) {
                for (int j = 0; j < 9; j++) {
                    switch (j) {
                    case 0:
                    case 8:
                        board[i][j] = new carChess(color, i, j);
                        break;
                    case 1:
                    case 7:
                        board[i][j] = new horseChess(color, i, j);
                        break;
                    case 2:
                    case 6:
                        board[i][j] = new elephantChess(color, i, j);
                        break;
                    case 3:
                    case 5:
                        board[i][j] = new chapChess(color, i, j);
                        break;
                    case 4:
                        board[i][j] = new generalChess(color, i, j);
                    }
                }
            }
            if (i == 2) {
                int[] cannon = { 1, 7 };
                for (int j = 0; j < cannon.length; j++)
                    board[i][cannon[j]] = new cannonChess(color, i, cannon[j]);
            }
            if (i == 3) {
                int[] soldier = { 0, 2, 4, 6, 8 };
                for (int j = 0; j < soldier.length; j++)
                    board[i][soldier[j]] = new soldierChess(color, i, soldier[j]);
            }
        }
        for (int i = 5; i < board.length; i++) {
            chess.Color color = chess.Color.RED;
            if (i == 9) {
                for (int j = 0; j < 9; j++) {
                    switch (j) {
                    case 0:
                    case 8:
                        board[i][j] = new carChess(color, i, j);
                        break;
                    case 1:
                    case 7:
                        board[i][j] = new horseChess(color, i, j);
                        break;
                    case 2:
                    case 6:
                        board[i][j] = new elephantChess(color, i, j);
                        break;
                    case 3:
                    case 5:
                        board[i][j] = new chapChess(color, i, j);
                        break;
                    case 4:
                        board[i][j] = new generalChess(color, i, j);
                    }
                }
            }
            if (i == 7) {
                int[] cannon = { 1, 7 };
                for (int j = 0; j < cannon.length; j++)
                    board[i][cannon[j]] = new cannonChess(color, i, cannon[j]);
            }
            if (i == 6) {
                int[] soldier = { 0, 2, 4, 6, 8 };
                for (int j = 0; j < soldier.length; j++)
                    board[i][soldier[j]] = new soldierChess(color, i, soldier[j]);
            }
        }
    }

    public void printBoard(){
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++)
                if (board[i][j] != null)
                    System.out.print(board[i][j].getName() + " ");
                else
                    System.out.print("空 ");
            System.out.println();
        }
    }

    public void moveChess(int x1,int y1,int x2,int y2,boolean isSave){
        if(rangeSense.canMove(this.board,x1,y1,x2,y2,this.turn)==true){
            this.board[x2][y2]=this.board[x1][y1];
            this.board[x2][y2].point=new Point(x2,y2);
            this.board[x1][y1]=null;
            if(this.turn== chess.Color.RED)
                this.turn= chess.Color.BlACK;
            else
                this.turn= chess.Color.RED;
            if(isSave)
                i.saveMove(new information(x1,y1,x2,y2));
        }
    }

    public void noJustiyMove(int x1,int y1,int x2,int y2){
        this.board[x2][y2]=this.board[x1][y1];
        this.board[x2][y2].point=new Point(x2,y2);
        this.board[x1][y1]=null;
        if(this.turn== chess.Color.RED)
            this.turn= chess.Color.BlACK;
        else
            this.turn= chess.Color.RED;
    }
    public static float getWindowWidth() {
        return getDeviceCapability().width * getDeviceCapability().screenDensity / 160f;
    }

    // 获取屏幕的高度
    public static float getWindowHeight() {
        return getDeviceCapability().height * getDeviceCapability().screenDensity / 160f;
    }
    private static DeviceCapability getDeviceCapability() {
        return MyApplication.context.getResourceManager().getDeviceCapability();
    }

    public chess[][] flip(){
        chess[][] flipBoard=new chess[board.length][board[0].length];
        for(int i=0;i<board.length;i++)
            for(int j=0;j<board[0].length;j++)
                flipBoard[i][j]=board[board.length-i-1][board[0].length-1-j];
            return  flipBoard;
    }

    enum state{
        SELECT,MOVE
    }
    public interface  I{
        void saveMove(information information);
    }
}